<template>
  <div id="pingjia">
      <div v-if="pinggu&&pinggu.length>0">
      <h1>民主生活会单位（部门）质量评估
        <el-popover
    ref="popover"
    placement="top-start"
    title="功能提示"
    width="300"
    trigger="hover"
    content="对系统评估显示为[需人工审核]的评估项，如果[人事司评估]列没有值，则一键赋默认值。">
  </el-popover>
       <!-- <el-button v-popover:popover style="float:right;margin-right:20px;margin-bottom:5px" type="primary" icon="el-icon-success" v-if="canedit" @click="yijianShenHe" :loading="yijianing">一键默认</el-button> -->
        </h1>
        <!--修改界面-->
        <el-dialog title="修改评估信息" :visible.sync="pingguEditFormVisible" >
          <el-form :model="pingguEditForm" label-width="120px" :rules="pingguEditFormRules" ref="pingguEditForm">
            <el-form-item label="阶段" prop="Jieduan">
              <b>{{pingguEditForm.Jieduan}}</b>
            </el-form-item>
            <el-form-item label="内容" prop="Neirong">
              <b>{{pingguEditForm.Neirong}}</b>
            </el-form-item>
            <el-form-item label="标准" prop="BiaozhunTitle" v-if="pingguEditForm.BiaozhunTitle">
              <b>{{pingguEditForm.BiaozhunTitle}}</b>
            </el-form-item>
            <el-form-item label="系统评估" prop="SysValue" v-if="pingguEditForm.BiaozhunTitle">
              <b>
                <span v-if="pingguEditForm.SysValue">
                  {{pingguEditForm.SysValue}}
                  <span v-if="pingguEditForm.ValueUnit">{{pingguEditForm.ValueUnit}}</span>
                </span>
              </b>
            </el-form-item>
            <el-form-item label="人事司评估" prop="Value" v-if="pingguEditForm.BiaozhunTitle" style="margin-bottom:10px;">
              <div v-if="pingguEditForm.Options">
                <el-radio-group v-model="pingguEditForm.Value">
                  <el-radio v-for="(item,index) in pingguEditForm.Options" :key="index" :label="item">{{item}}</el-radio>
                </el-radio-group>
              </div>
              <div v-else-if="pingguEditForm.DataType==='Int'">
                <el-input-number :precision="0" v-model="pingguEditForm.Value" auto-complete="off"></el-input-number>{{pingguEditForm.ValueUnit}}
              </div>
              <div v-else-if="pingguEditForm.DataType==='Date'">
                <el-date-picker v-model="pingguEditForm.Value" auto-complete="off" type="date" placeholder="选择日期"></el-date-picker>
              </div>
              <div v-else>
                <el-input v-model="pingguEditForm.Value" auto-complete="off"></el-input>
              </div>
            </el-form-item>
            <el-form-item label="评估建议" prop="Yiju" v-else>
              <el-input v-model="pingguEditForm.Yiju" auto-complete="off" type="textarea"></el-input>
            </el-form-item>
            <el-form-item label="支撑依据" prop="Yiju" v-if="pingguEditForm.BiaozhunTitle">
              <el-input v-model="pingguEditForm.Yiju" auto-complete="off" type="textarea"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click.native="pingguEditFormVisible = false">取消</el-button>
            <el-button type="primary" @click.native="pingguEditFormSubmit" :loading="pingguEditFormLoading">保存</el-button>
          </div>
        </el-dialog>
        <el-table :data="pinggu" v-loading="listLoading" style="width: 100%;" border :highlight-current-row="false" :span-method="objectOneMethod">
          <el-table-column type="index" label="序号" width="70" align="center">
          </el-table-column>
          <el-table-column prop="Jieduan" label="阶段" width="100">
          </el-table-column>
          <el-table-column prop="Neirong" label="内容" width="150">
          </el-table-column>
          <el-table-column prop="BiaozhunTitle" label="标准">
            <template slot-scope="scope">
              <template v-if="scope.row.Neirong==='督导组手动上传质量评估表'">
                <el-upload
                  ref="upload"
                  :action="scope._self.$http.defaults.baseURL+scope._self.$http.defaults.shenghuohuiAPI +'api/newFileInfo/upload/'"
                  :on-success="handleUploadSucc"
                  :on-error="uploadError"
                  :before-upload="beforeUpload"
                  :show-file-list="false"
                >
                  <el-button type="primary">上传附件</el-button>
                </el-upload>
                <div v-for="(item,index) in files" :key="'file_'+index">
                  <a href="javascript:void(0)" @click="download(item.id)">{{item.fileName}}.{{item.fileType}}</a>
                  <el-popconfirm title="确定删除吗？" @confirm="removeFile(item.id)"><el-button slot="reference" type="text" icon="el-icon-delete" /></el-popconfirm>
                  <br>
                </div>
              </template>
              <template v-else>
                <span v-if="scope.row.BiaozhunTitle">{{scope.row.BiaozhunTitle}}</span>
                <span v-else>{{scope.row.Yiju}}</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="系统评估" width="150">
            <template slot-scope="scope">
              <el-button icon="el-icon-more" v-if="scope.row.Link1" circle size="mini" slot="reference" @click="changetab(scope.row.Link1,scope.row.Link2)"></el-button>
              <span v-if="scope.row.SysValue">
                <span v-if="scope.row.SysValue==='需人工审核'" style="color:red">{{scope.row.SysValue}}</span>
                <span v-else>
                  {{scope.row.SysValue}}
                </span>
                <span v-if="scope.row.ValueUnit">{{scope.row.ValueUnit}}</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="人事司评估" width="200">
            <template slot-scope="scope">
              <el-popover v-if="scope.row.Yiju"
                placement="top-start"
                title="支撑依据"
                width="200"
                trigger="hover"
                :content="scope.row.Yiju">
                  <el-button icon="el-icon-search" circle size="mini" slot="reference"></el-button>
              </el-popover>
                {{scope.row.Value}}
                <span v-if="scope.row.ValueUnit">{{scope.row.ValueUnit}}</span>
                <div v-if="pinggutishi(scope.row.BiaoZhun,scope.row.Value)" style="float:right;">
                  <img src="../../assets/error.png" style="width:27px;height:27px" />
                </div>
            </template>
          </el-table-column>
          <!-- <el-table-column label="操作" width="150" v-if="canedit">
            <template slot-scope="scope">
              <el-button size="small" @click="pingguEdit(scope.row.Id)" type="primary">修改</el-button>
            </template>
          </el-table-column> -->
        </el-table>
          <div style="clear:both; float:right;margin:20px;" v-if="pinggu.length>0">
            <el-button type="primary" v-if="files&&files.length>0" @click.native="daochuPingGuFujian" :loading="exportloading">导出该单位手动上传的民主生活会评估表</el-button>
            <!-- <el-button type="success" @click.native="daochuPingGu" :loading="exportloading">导出该单位系统生成的民主生活会评估表</el-button> -->
          </div>
      </div>
      <div v-else style="padding:20px; font-size:14px;color:#999;text-align:center">暂无数据</div>
  </div>
</template>
<script>
export default {
  props: {
    timestamp: ''
  },
  data () {
    return {
      files: [],
      options12: ['全部审阅', '部分审阅', '未审阅'],
      options15: ['全部遵循', '部分遵循', '未遵循'],
      options18: ['全部到会', '达到2/3', '不到2/3'],
      qitadata: [],
      listLoading: false,
      pingjia: {},
      pinggu: [],
      ziping: {},
      canedit: false,
      pingjialoading: false,
      exportloading: false,
      pingguEditFormVisible: false,
      pingguEditForm: {},
      pingguEditFormLoading: false,
      pingguEditFormRules: {},
      quexishu: '',
      yijianing: false
    }
  },
  watch: {
    timestamp (val) {
      this.loaddata()
    }
    // $refs.tree2.getCurrentNode()
  },
  methods: {
    removeFile (id) {
      const url = 'api/newFileInfo/remove?real=true&id=' + id
      this.$http.get(this.$http.defaults.shenghuohuiAPI + url).then(res => {
        console.log('删除成功:', res)
        if (res.data.success === true) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
          this.getFiles()
        }
      })
    },
    download (id) {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.shenghuohuiAPI + 'api/newFileInfo/download?download=true&id=' + id + '&t=' + new Date())
    },
    getFiles () {
      this.$http.get(this.$http.defaults.shenghuohuiAPI + `api/shenghuohuiunit/getPingguFiles?id=` + this.$route.params.id).then(response => {
        if (response.data.success === true) {
          this.files = response.data.result
        }
      }).catch((error) => {
        this.$message({
          message: '网络获取附件列表失败:' + error,
          type: 'error'
        })
      })
    },
    uploadError (err) {
      console.log(err)
      this.$message({
        message: '上传失败:' + err,
        type: 'error'
      })
    },
    beforeUpload (file) {
      console.log('文件类型')
      console.log(file.name)
      const isLt500M = file.size / 1024 / 1024 < 60
      if (!isLt500M) {
        this.$message.error('单个附件不能超过60M!')
      }
      return isLt500M
    },
    handleUploadSucc (response, file, fileList, flag) {
      if (response.success) {
        let para = {
          'files': [response.result[0].id],
          'shenghuohuiunitId': this.$route.params.id
        }
        this.$http.post(this.$http.defaults.shenghuohuiAPI + `api/shenghuohuiunit/updatePingguFile`, para).then((response) => {
          if (response.data.success === true) {
            this.$message({
              message: '上传成功！',
              type: 'success'
            })
            this.getFiles()
          } else {
            this.$message({
              message: '上传失败:' + response.data.message,
              type: 'error'
            })
          }
        }).catch(error => {
          console.log(error)
          this.pingguEditFormLoading = false
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
      } else {
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error'
        })
      }
    },
    pinggutishi (biaozhun, value) {
      if (biaozhun) {
        if (value) {
          if (biaozhun === 'BZ_SHI') {
            if (value === '是') {
              return false
            } else {
              return true
            }
          }
          if (biaozhun === 'BZ_FEIKONG') {
            if (value !== '') {
              return false
            } else {
              return true
            }
          }
          if (biaozhun === 'BZ_DAYULING') {
            if (parseInt(value) > 0) {
              return false
            } else {
              return true
            }
          }
          if (biaozhun === 'BZ_DENGYULING') {
            if (value === '0') {
              return false
            } else {
              return true
            }
          }
          if (biaozhun === 'BZ_DENGYUYIBAI') {
            if (value === '100') {
              return false
            } else {
              return true
            }
          }
          if (biaozhun === 'BZ_QUANDAOHUI') {
            if (value === '全部到会' || value === '达到2/3') {
              return false
            } else {
              return true
            }
          }
          if (biaozhun === 'BZ_DENGYUQUEXI') {
            if (value === this.quexishu) {
              return false
            } else {
              return true
            }
          }
        } else {
          return true
        }
      } else {
        return false
      }
    },
    changetab: function (link1, link2) {
      this.$el.scrollIntoView()
      // document.getElementById('pingjia').scrollIntoView()
      let params1 = {Link1: link1}
      this.$store.commit('tablink1', params1)
      console.log('link1:' + link1)
      if (link1 === 'Huiqian') {
        console.log('link_hq:' + link2)
        let paramsHq = {Link_hq: link2}
        this.$store.commit('tablink_hq', paramsHq)
      }
      if (link1 === 'Huishang') {
        console.log('link_hz:' + link2)
        let paramsHz = {Link_hz: link2}
        this.$store.commit('tablink_hz', paramsHz)
      }
      if (link1 === 'Huihou') {
        console.log('link_hh:' + link2)
        let paramsHh = {Link_hh: link2}
        this.$store.commit('tablink_hh', paramsHh)
      }
    },
    pingguEditFormSubmit: function () {
      var _this = this
      this.$refs.pingguEditForm.validate((valid) => {
        if (valid) {
          _this.pingguEditFormLoading = true
          let para = {
            id: this.pingguEditForm.Id,
            yiju: this.pingguEditForm.Yiju,
            value: this.pingguEditForm.Value
          }
          this.$http.post(`/shenghuohui/editpinggu`, para).then(function (response) {
            console.log(response.data.IsSuccess)
            if (response.data.IsSuccess === true) {
              _this.pingguEditFormLoading = false
              _this.$message({
                message: '保存成功',
                type: 'success'
              })
              _this.pingguEditFormVisible = false
              _this.loaddata()
            } else {
              _this.pingguEditFormLoading = false
              console.log('保存失败:' + response.data.Msg)
              _this.$message({
                message: '保存失败:' + response.data.Msg,
                type: 'error'
              })
            }
          }).catch(function (error) {
            console.log(error)
            _this.pingguEditFormLoading = false
            _this.$message({
              message: '获取失败:' + error,
              type: 'error'
            })
          })
        }
      })
    },
    pingguEdit: function (id) {
      console.log('开启修改界面' + id)
      this.getpingguDetail(id)
    },
    yijianShenHe: function () {
      let _this = this
      _this.yijianing = true
      let para = {
        id: this.$route.params.id
      }
      this.$http.post(`/shenghuohui/yijianshenhe`, para).then(function (response) {
        console.log(response.data.IsSuccess)
        if (response.data.IsSuccess === true) {
          _this.yijianing = false
          _this.$message({
            message: response.data.Msg,
            type: 'success'
          })
          _this.loaddata()
        } else {
          _this.yijianing = false
          console.log('一键默认失败:' + response.data.Msg)
          _this.$message({
            message: '一键默认失败:' + response.data.Msg,
            type: 'error'
          })
        }
      }).catch(function (error) {
        console.log(error)
        _this.yijianing = false
        _this.$message({
          message: '一键默认失败:' + error,
          type: 'error'
        })
      })
    },
    getpingguDetail: function (id) {
      for (var i = 0; i < this.pinggu.length; i++) {
        if (this.pinggu[i].Id === id) {
          let objData = Object.assign({}, this.pinggu[i])
          this.pingguEditForm = objData
          console.log('类型：' + this.pingguEditForm.DataType)
          this.pingguEditFormVisible = true
          break
        }
      }
    },
    setTable () {
      let spanOneArr = []
      let spanTwoArr = []
      let concatOne = 0
      let concatTwo = 0
      let tableData = this.pinggu
      tableData.forEach((item, index) => {
        if (index === 0) {
          spanOneArr.push(1)
          spanTwoArr.push(1)
        } else {
          if (item.Jieduan === tableData[index - 1].Jieduan) { // 第一列需合并相同内容的判断条件
            spanOneArr[concatOne] += 1
            spanOneArr.push(0)
          } else {
            spanOneArr.push(1)
            concatOne = index
          }
          if (item.Neirong === tableData[index - 1].Neirong) { // 第二列和第16列需合并相同内容的判断条件
            spanTwoArr[concatTwo] += 1
            spanTwoArr.push(0)
          } else {
            spanTwoArr.push(1)
            concatTwo = index
          }
        }
      })
      return {
        one: spanOneArr,
        two: spanTwoArr
      }
    },
    setTable2 () {
      let rowindex = []
      let tableData = this.pinggu
      tableData.forEach((item, index) => {
        if (!item.BiaozhunTitle) {
          rowindex.push(index)
        }
      })
      return rowindex
    },
    objectOneMethod ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 1) {
        const _row = (this.setTable(this.pinggu).one)[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
      if (columnIndex === 2) {
        const _row = (this.setTable(this.pinggu).two)[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
      let indexs = this.setTable2()
      for (var i = 0; i < indexs.length; i++) {
        if (rowIndex === indexs[i] && columnIndex === 3) {
          return {
            rowspan: 1,
            colspan: 3
          }
        }
        if (rowIndex === indexs[i] && columnIndex === 4) {
          return {
            rowspan: 1,
            colspan: 0
          }
        }
        if (rowIndex === indexs[i] && columnIndex === 5) {
          return {
            rowspan: 1,
            colspan: 0
          }
        }
      }
    },
    daochuPingGuFujian: function () {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.shenghuohuiAPI + `api/shenghuohuiunit/getUnitPingguFiles?shenghuohuiunitId=` + this.$route.params.id)
    },
    daochuPingGu: function () {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.shenghuohuiAPI + `api/shenghuohui/DownloadUnit?id=` + this.$route.params.id)
    },
    loaddata: function () {
      this.listLoading = true
      // let para = {
      //   id: this.$route.params.id
      // }
      // var personParams = { Page: 0, Pagenum: 20, Personname: '', Unitkey: '', Sortkey: '', Isace: true, timestamp: new Date().getTime() }
      this.$http
        .get(this.$http.defaults.shenghuohuiAPI + `api/shenghuohui/unitPingGu?unitId=` + this.$route.params.id)
        // .post(`/shenghuohui/unitpinggu`, para)
        .then(response => {
          this.listLoading = false
          if (response.data.success === true) {
            this.pinggu = response.data.result.PingGu
            if (this.pinggu.length > 0) {
              this.pinggu.push({BiaoZhun: 'BZ_FEIKONG', BiaozhunTitle: '', DataType: 'String', Id: 620, Jieduan: '督导评估意见', Link1: 'Huiqian', Link2: 'Fangan', Name: 'A29', Neirong: '督导组手动上传质量评估表'})
            }
            this.ziping = response.data.result.Ziping
            this.canedit = response.data.result.CanEdit
            this.quexishu = response.data.result.Quexishu
            this.getFiles()
          } else {
            this.$message({
              message: '获取数据失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
          this.listLoading = false
        })
    }
  }
}
</script>
<style scoped>
.el-form-item{
  margin:0;
}
</style>
<style>
#pingjia .el-table--border td,#pingjia .el-table--border th,#pingjia .el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed{
  border-right:1px solid #1680C7 !important;
}
#pingjia .el-table--border th,#pingjia .el-table__fixed-right-patch,#pingjia .el-table td,#pingjia .el-table th.is-leaf{
  border-bottom:1px solid #1680C7 !important;
}
#pingjia .el-table--border,#pingjia .el-table--group{
  border:1px solid #1680C7 !important;
}
.el-popover--plain{
  color:red;
}
</style>
